package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 16778
 * @date 2025/9/29 10:31
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @LogOperation
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping
    public Result page(@RequestParam(required = false) String name,
                       @RequestParam(required = false) Integer status,
                       Integer page,
                       Integer pageSize){
        log.info("部门查询,参数:name:{},status:{},page:{},pageSize:{}",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.page(name,status,page,pageSize);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Dept dept = deptService.getInfo(id);
        return Result.success(dept);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改后的部门数据:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

    @LogOperation
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除部门id:{}",id);
        deptService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Dept> list = deptService.findAll();
        return Result.success(list);
    }
}
